/*

Copyright 2014年10月9日 tngou



Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at



    http://www.apache.org/licenses/LICENSE-2.0



Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

*/
package net.tngou.c4j.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import net.tngou.c4j.model.RegexUrl;
import net.tngou.c4j.model.Url;
import net.tngou.crawler.util.StringUtil;

/**
 * Url 匹配
 * @author tngou@tngou.net
 *
 */
public class UrlRegex {

	public Set<Url> filterUrl(Set<Url> urls,List<RegexUrl> regexs) {
		Set<Url> list = new HashSet<>();
		urls.forEach(e->{
			
			Url url = matches(e, regexs);
			if(url!=null)list.add(url);
		});
		return list;
	}
	
	public boolean filterUrl(String url,String regex) {
		return Pattern.matches(regex, url);
	}
	public boolean filterUrl(String url,List<String> regexs) {
		if(StringUtil.isEmpty(url)||regexs==null||regexs.isEmpty())return false;
		for (String regex : regexs) {
			if(Pattern.matches(regex, url))return true;
		}
		return false;
	}
	public Url matches(Url url ,List<RegexUrl> regexs) {
		for (RegexUrl regex : regexs) {
			if(Pattern.matches(regex.getUrl(), url.toString()))
			{
				url.setId(regex.getId());
				url.setType(regex.getType());
				return url;
			}
		}
		return null;
	}
	
	public String[] get(String url,String match) {
		String[] matchs = StringUtil.split(match,"${}");
		for (String m : matchs) {
			url=StringUtil.replace(url, m, "|", 1);
		}	
		return StringUtil.split(url,"|");
	}
	public Set<Url> filterUrl(Set<Url> urls,String surl) {
		Set<Url> list = new HashSet<>();
		try {
			URL url = new URL(surl);	
			urls.forEach(e->{
				if(e.getUrl().indexOf(url.getHost())>=0)list.add(e);
			});		
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		return list;
	}

	
	public Set<Url> outfilterUrl(Set<Url> urls,String regex) {
		
		Iterator<Url> it = urls.iterator();
        for(int i=0; i<urls.size(); i++){
 
        	Url e = it.next();
            if(filterUrl(e.getUrl(), regex)){
                it.remove();
                i--;
            }
        }		
		return urls;
	}
	
	public Set<Url> outfilterUrl(Set<Url> urls,List<String> regexs) {
		for (String regex : regexs) {
			urls=outfilterUrl(urls, regex);			
		}			
		return urls;
	}
	
	public static void main(String[] args) throws MalformedURLException {
		UrlRegex regex = new UrlRegex();
//		URL url = new URL("http://health.qq.com/winds/jkzx.htm1");
		System.err.println(regex.filterUrl("http://www.360kad.com/prsoduct/39353.shtml?1212121212121212121212121212121","^http://www.360kad.com/product/+(.*)"));
//		boolean b = Pattern.matches("http://health.qq.com/winds/jkzx.htm1", "http://health.qq.com/winds/jkzx.htm");
//		System.err.println(b);
	}
}
